package com.ccn.crm.util;

import com.alibaba.fastjson.JSON;

import java.rmi.MarshalledObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通过userId
 * 获取用户名字
 */
public class GetUserInfo {
    public static Map<String, Object> GetUserInfo(List<Object> userId , String token) {
        String id = (String) userId.get(0);

        String url = "https://open.fxiaoke.com/cgi/user/get";
        Map<String, Object> jsonObject = new HashMap<String, Object>();
        //设置访问参数
        jsonObject.put("corpId","FSCID_A6D390D2E5FFF878491C1F5E6DA2994B");
        jsonObject.put("corpAccessToken",token);
        jsonObject.put("openUserId",id);
        //把参数转化为字符串
        String json = JSON.toJSONString(jsonObject);
        //调用工具进行跨域请求获取token的corpID等参数
        String post = HttpUtil.httpProxy(url, json, "post", "application/json");
        //  String str="{\"corpAccessToken\":\"7129AE8DA6F9C5188C40BA920118A0E2\",\"corpId\":\"FSCID_A6D390D2E5FFF878491C1F5E6DA2994B\",\"expiresIn\":2422,\"errorCode\":0,\"errorMessage\":\"success\"}\n";
        //将获取的参数转化成map类型
        HashMap userInfo = JSON.parseObject(post, HashMap.class);
        return userInfo;
    }
    public static String GetUserId(List<Object> userName , Map<String, Object> token) {
        String name = (String) userName.get(0);
        String url = "https://open.fxiaoke.com/cgi/user/getByNickName";
        Map<String, Object> jsonObject = new HashMap<String, Object>();
        String corpAccessToken = (String) token.get("corpAccessToken");
        String corpId = (String) token.get("corpId");
        //设置访问参数
        jsonObject.put("corpId", corpId);
        jsonObject.put("corpAccessToken", corpAccessToken);
        jsonObject.put("nickName", name);
        //把参数转化为字符串
        String json = JSON.toJSONString(jsonObject);
        //调用工具进行跨域请求获取token的corpID等参数
        String post = HttpUtil.httpProxy(url, json, "post", "application/json");
        //  String str="{\"corpAccessToken\":\"7129AE8DA6F9C5188C40BA920118A0E2\",\"corpId\":\"FSCID_A6D390D2E5FFF878491C1F5E6DA2994B\",\"expiresIn\":2422,\"errorCode\":0,\"errorMessage\":\"success\"}\n";
        //将获取的参数转化成map类型
        HashMap userInfo = JSON.parseObject(post, HashMap.class);
        if (!userInfo.isEmpty()) {
            List<Map<String, Object>> empList = (List<Map<String, Object>>) userInfo.get("empList");
            System.out.println(empList);
            if (null!=empList&&empList.size() > 0) {
                if (null != empList.get(0).get("openUserId")) {
                    String leaderId = (String) empList.get(0).get("openUserId");
                    return leaderId;
                } else {
                    return null;
                }
            } else {
                return null;
            }

        }
        return null;
    }
    public static  Map<String, Object> GetUserinfoByRealname(List<Object> userName , Map<String, Object> token) {
        String name = (String) userName.get(0);
        String url = "https://open.fxiaoke.com/cgi/user/getByNickName";
        Map<String, Object> jsonObject = new HashMap<String, Object>();
        String corpAccessToken = (String) token.get("corpAccessToken");
        String corpId = (String) token.get("corpId");
        //设置访问参数
        jsonObject.put("corpId", corpId);
        jsonObject.put("corpAccessToken", corpAccessToken);
        jsonObject.put("nickName", name);
        //把参数转化为字符串
        String json = JSON.toJSONString(jsonObject);
        //调用工具进行跨域请求获取token的corpID等参数
        String post = HttpUtil.httpProxy(url, json, "post", "application/json");
        //  String str="{\"corpAccessToken\":\"7129AE8DA6F9C5188C40BA920118A0E2\",\"corpId\":\"FSCID_A6D390D2E5FFF878491C1F5E6DA2994B\",\"expiresIn\":2422,\"errorCode\":0,\"errorMessage\":\"success\"}\n";
        //将获取的参数转化成map类型
        HashMap userInfo = JSON.parseObject(post, HashMap.class);

        return userInfo;
    }
}
